package com.jdy.userservice.controller;

import com.jdy.userservice.entity.SysUser;
import com.jdy.userservice.service.SysUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags="测试投票")
@CrossOrigin //跨域
@RestController
@RequestMapping("/sys-user")
public class MyController {

    @Autowired
    SysUserService service;

    @ApiOperation(value = "赛事安排-搜索接口", notes = " \n author: liuqichun 2021/3/28")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "competitionId", value = "赛事id", paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "criteria", value = "标准查询字段", paramType = "query", dataType = "CompetitionScheduleQueryCriteria"),
            @ApiImplicitParam(name = "pageVO", value = "分页对象", paramType = "query", dataType = "PageVO")
    })
    @PostMapping("/login")
    public String login(@RequestParam String username,@RequestParam String password){
        System.out.println("00000000000000000");
        System.out.println(username);
        System.out.println(password);
        //用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
        } catch (UnknownAccountException e) {
            log.error("用户名不存在！", e);
            return "用户名不存在！";
        } catch (AuthenticationException e) {
            log.error("账号或密码错误！", e);
            return "账号或密码错误！";
        } catch (AuthorizationException e) {
            log.error("没有权限！", e);
            return "没有权限";
        }
        return "login success";
    }

    @GetMapping("/regist")
    public void regist(@RequestParam String username,@RequestParam String password){
         SysUser sysUser =new SysUser();
         sysUser.setPassword(password);
//         注解
         sysUser.setUsername(username);
        boolean save = service.save(sysUser);

    }



    @GetMapping("/loginout")
    public void index(){
        SysUser admin = service.getPassword("admin");
        System.out.println("admin");
        System.out.println(admin);
    }
}
